﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;
using System.Data;

namespace VswService_V7.OPC.DB
{
	public class FcoAddressDbData:SubscriptionDbDataBase
	{
		public FcoAddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc,nodes)
		{
            fcoPipe = new PipeInformation();
            fcoPipe.location = FCOLocation;
        }

		string lastStripNo = "";
        string FCOLocation = "FCO";
        PipeInformation fcoPipe;
		
		public override void CreateSubscription()
		{
            try
            {
                opcServer.QuickAddMonitorItem(opcNodes.FcoAddress.GrpoupName, 
                    opcNodes.FcoAddress.PublishTime, 
                    opcNodes.FcoAddress.SampleTime,
                    opcNodes.FcoAddress.GetNodeAddress(), 
                    opcNodes.FcoAddress.GetNodeDisplayName(),
                    SubscriptionHandle_FcoAddress);

                opcServer.QuickAddMonitorItem(opcNodes.FcoPipeInfoAddress.GrpoupName,
                    opcNodes.FcoPipeInfoAddress.PublishTime,
                    opcNodes.FcoPipeInfoAddress.SampleTime,
                    opcNodes.FcoPipeInfoAddress.GetNodeAddress(),
                    opcNodes.FcoPipeInfoAddress.GetNodeDisplayName(),
                    SubscriptionHandle_FcoPipeInfoAddress);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("FcoAddressDbData CreateSubscription", ex);
            }
			
		}
		
		private void SubscriptionHandle_FcoAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
		{
			MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
			if (notification == null || notification.Value.Value == null)
				return;
			if (monitoredItem.DisplayName == "FcoAsk")
			{
				bool ask;
				if (bool.TryParse(notification.Value.Value.ToString(), out ask))
				{
					if (ask)
					{
                        Console.WriteLine("fco new pipe: ");
                        Task.Run(() => HandleNewPipe());											
					}
					else
					{
                        Console.WriteLine("fco new pipe reply");
                        opcServer.WriteValue("false",opcNodes.FcoAddress.FcoRsp);
					}
				}
			}
		}

		private void SubscriptionHandle_FcoPipeInfoAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
		{
			MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
			if (notification == null || notification.Value.Value == null)
				return;
			switch (monitoredItem.DisplayName)
			{
				case "Strip_No":
					{
                        fcoPipe.stripNo = notification.Value.Value.ToString();
						break;
					}
				case "Pipe_No":
					{
                        fcoPipe.pipeNo = notification.Value.Value.ToString();
                        Console.WriteLine(FCOLocation + " new pipeNo " + fcoPipe.pipeNo);
                        break;
					}
				case "Batch_No":
					{
                        fcoPipe.batchNo = notification.Value.Value.ToString();
                        //Console.WriteLine(FCOLocation + " new batchNo " + fcoPipe.batchNo);
                        break;
					}
				case "Strip_Seq":
					{
                        fcoPipe.stripSeq = notification.Value.Value.ToString();
						break;
					}
				case "Location":
					{
                        //fcoPipe.location = notification.Value.Value.ToString();
						break;
					}
				case "Dtj":
					{
                        fcoPipe.dtj = notification.Value.Value.ToString();
						break;
					}
				case "Length":
					{
                        fcoPipe.length = notification.Value.Value.ToString();
						break;
					}
				case "Quality_Code":
					{
                        fcoPipe.qualityCode = notification.Value.Value.ToString();
						break;
					}

				case "Quality_Code2":
					{
						fcoPipe.qualityCode2 = notification.Value.Value.ToString();
						break;
					}

				case "SampleReason":
                    {
                        fcoPipe.sampleReason = notification.Value.Value.ToString();
                        break;
                    }

                case "FLTLength":
                    {
                        fcoPipe.flt_length = notification.Value.Value.ToString();
                        break;
                    }

                case "MACLength":
                    {
                        fcoPipe.machinal_length = notification.Value.Value.ToString();
                        break;
                    }

                case "ADDLength":
                    {
                        fcoPipe.addition_length = notification.Value.Value.ToString();
                        break;
                    }

				case "HeadCut":
					{
						fcoPipe.headCut = notification.Value.Value.ToString();
						break;
					}

				case "TailCut":
					{
						fcoPipe.tailCut = notification.Value.Value.ToString();
						break;
					}



				default:
					{
						break;
					}
			}			
		}


        private async Task HandleNewPipe()
        {
            await Task.Delay(2000);
            Console.WriteLine("fco new pipe start record");

            fcoPipe.updateTime = DateTime.Now.ToString(Formats.DataFormat);
            try
            {
                if (!string.IsNullOrEmpty(fcoPipe.batchNo) && !string.IsNullOrEmpty(fcoPipe.pipeNo) && fcoPipe.batchNo != "0" && fcoPipe.pipeNo != "0")
                {
					PIPE_INFO.InsertNewPipeInfoRecord(fcoPipe);
				}
                
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("HandleNewPipe", ex);
            }            
            opcServer.WriteValue("true", opcNodes.FcoAddress.FcoRsp);
        }
	}

}
